package cn.wanda.bi.sunshine.platform.server.service;

import cn.wanda.bi.sunshine.platform.server.aspect.Logging;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.LabelManagementRuleDetailMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.LabelManagementRuleMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.*;
import cn.wanda.bi.sunshine.platform.server.controller.request.LabelManagementRuleRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangfupeng1
 */
@Service
public class LabelManagementRuleService implements Logging {

    public static final Logger logger = LoggerFactory.getLogger(LabelManagementRuleService.class);


    @Autowired
    private LabelManagementRuleMapper labelManagementRuleMapper;
    @Autowired
    private LabelManagementRuleDetailMapper labelManagementRuleDetailMapper;

    //增加
    public SimpleResponse<LabelManagementRuleRequest> add(LabelManagementRuleRequest labelManagementRuleRequest) {
         labelManagementRuleMapper.insertSelective(labelManagementRuleRequest.getLabelManagementRule());
        List<LabelManagementRuleDetail> labelManagementRuleDetails = labelManagementRuleRequest.getLabelManagementRuleDetail();
        for (LabelManagementRuleDetail labelManagementRuleDetail:labelManagementRuleDetails
             ) {
            labelManagementRuleDetail.setRuleId(labelManagementRuleRequest.getLabelManagementRule().getId());
            labelManagementRuleDetailMapper.insertSelective(labelManagementRuleDetail);
        }
        return SimpleResponse.success(labelManagementRuleRequest);
    }

    //根据PageId进行查询
    public SimpleResponse<LabelManagementRuleRequest> queryById(Integer id) {
        LabelManagementRule labelManagementRule = labelManagementRuleMapper.selectByPrimaryKey(id);
        LabelManagementRuleDetailExample labelManagementRuleDetailExample = new LabelManagementRuleDetailExample();
        labelManagementRuleDetailExample.createCriteria().andRuleIdEqualTo(labelManagementRule.getId());
        List<LabelManagementRuleDetail> labelManagementRuleDetails = labelManagementRuleDetailMapper.selectByExample(labelManagementRuleDetailExample);
        LabelManagementRuleRequest labelManagementRuleRequest = new LabelManagementRuleRequest();
        labelManagementRuleRequest.setLabelManagementRule(labelManagementRule);
        labelManagementRuleRequest.setLabelManagementRuleDetail(labelManagementRuleDetails);
        return SimpleResponse.success(labelManagementRuleRequest);
    }

	//根据page id进行修改
	@CacheEvict(value = "cache:userLabelAuthMenu", key = "'cache:userLabelAuthMenu:'+#userLabelAuthMenu.getId()")
	public SimpleResponse<LabelManagementRuleRequest> update(LabelManagementRuleRequest labelManagementRuleRequest) {

        labelManagementRuleMapper.updateByPrimaryKeySelective(labelManagementRuleRequest.getLabelManagementRule());
        LabelManagementRuleDetailExample labelManagementRuleDetailExample = new LabelManagementRuleDetailExample();
        labelManagementRuleDetailExample.createCriteria().andRuleIdEqualTo(labelManagementRuleRequest.getLabelManagementRule().getId());
        labelManagementRuleDetailMapper.deleteByExample(labelManagementRuleDetailExample);
        List<LabelManagementRuleDetail> labelManagementRuleDetails = labelManagementRuleRequest.getLabelManagementRuleDetail();
        for (LabelManagementRuleDetail labelManagementRuleDetail:labelManagementRuleDetails
                ) {
            labelManagementRuleDetail.setRuleId(labelManagementRuleRequest.getLabelManagementRule().getId());
            labelManagementRuleDetailMapper.insertSelective(labelManagementRuleDetail);
        }
        return SimpleResponse.success(labelManagementRuleRequest);
    }
    //删除page
	@CacheEvict(value = "cache:userLabelAuthMenu", key = "'cache:userLabelAuthMenu:'+#userLabelAuthMenu.getId()")
    public SimpleResponse delete( Integer id) {
        labelManagementRuleMapper.deleteByPrimaryKey(id);
        LabelManagementRuleDetailExample labelManagementRuleDetailExample = new LabelManagementRuleDetailExample();
        labelManagementRuleDetailExample.createCriteria().andRuleIdEqualTo(id);
        labelManagementRuleDetailMapper.deleteByExample(labelManagementRuleDetailExample);
        return SimpleResponse.success();
    }
    //查询所有Page
    public ListResponse<LabelManagementRule> list(Integer index, Integer limit) {
        LabelManagementRuleExample labelManagementRuleExample = new LabelManagementRuleExample();
        labelManagementRuleExample.setOffset(index * limit - limit);
        labelManagementRuleExample.setLimit(limit);
        List<LabelManagementRule> labelManagementRules = labelManagementRuleMapper.selectByExample(labelManagementRuleExample);

        int count = labelManagementRuleMapper.countByExample(labelManagementRuleExample);
        return ListResponse.success(labelManagementRules,count,index,limit);
    }


}
